package zd.vrp.global;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import zd.vrp.utils.MyComparator;
import zd.vrp.utils.SortObject;



public class LoadData {
	public void read() {
		

		//File inFile = new File("data/C1_8_5.txt");
		File inFile = new File("data/C102.txt");

		//File inFile = new File("data/R110.txt");
		//File inFile = new File("data/RC101.txt");
		try {
			BufferedReader fr = new BufferedReader(new FileReader(inFile));
			String nextLine = fr.readLine();

			while (!"".equals(nextLine) && nextLine != null) {
				City city = getCity(nextLine);
				GlobalInformation.cityList.add(city);

				if (GlobalInformation.maxX == -1) {
					GlobalInformation.maxX = city.getX();
					GlobalInformation.minX = city.getX();
					GlobalInformation.maxY = city.getY();
					GlobalInformation.minY = city.getY();
				} else {
					if (city.getX() > GlobalInformation.maxX) {
						GlobalInformation.maxX = city.getX();
					}
					if (city.getX() < GlobalInformation.minX) {
						GlobalInformation.minX = city.getX();
					}
					if (city.getY() > GlobalInformation.maxY) {
						GlobalInformation.maxY = city.getY();
					}
					if (city.getY() < GlobalInformation.minY) {
						GlobalInformation.minY = city.getY();
					}
				}

				nextLine = fr.readLine();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		GlobalInformation.cityNum = GlobalInformation.cityList.size();
		GlobalInformation.unvisitedCity = GlobalInformation.cityNum-1;
		GlobalInformation.distance = new double[GlobalInformation.cityNum*GlobalInformation.cityNum];
		GlobalInformation.ranks = new int[GlobalInformation.cityNum*GlobalInformation.cityNum];
		calRankInfo();
	}
	
	//calculate rank information
	public void calRankInfo(){		
		
		for(int i=0;i<GlobalInformation.cityNum;i++){
			double[] tempArray = new double[GlobalInformation.cityNum];
			for(int j=0;j<GlobalInformation.cityNum;j++){
				City cityI = GlobalInformation.cityList.get(i);
				City cityJ = GlobalInformation.cityList.get(j);
				double d = 0.0;
				d += Math.pow((cityI.getX()-cityJ.getX()),2)+Math.pow((cityI.getY()-cityJ.getY()),2);
				d = Math.sqrt(d);
				GlobalInformation.distance[i*GlobalInformation.cityNum+j] = d;
				tempArray[j] = d;
			}
			int[] resultArray = sortArray(tempArray);
			for(int j=0;j<resultArray.length;j++){
				GlobalInformation.ranks[i*GlobalInformation.cityNum+j] = resultArray[j];
			}
		}
	}

	public static City getCity(String s) {

		City city = new City();
		if (!"".equals(s) && s != null) {
			Scanner sc = new Scanner(s);
			int id = sc.nextInt();
			
			double x = sc.nextDouble();
			double y = sc.nextDouble();			
			double demand = sc.nextDouble();			
			double etime = sc.nextDouble();			
			double ltime = sc.nextDouble();			
			double serviceTime = sc.nextDouble();
			
			city.setId(id);
			city.setX(x);
			city.setY(y);
			city.setDemand(demand);
			city.setEtime(etime);
			city.setLtime(ltime);
			city.setServiceTime(serviceTime);			
		}
		return city;
	}
	//sort an array
	public int[] sortArray(double[] inputArray){
		int size = inputArray.length;
		List<SortObject> tempList = new LinkedList<SortObject>();
		for(int i=0;i<size;i++){
			SortObject so = new SortObject();
			so.setPosition(i);
			so.setValue(inputArray[i]);
			tempList.add(so);
		}
		Collections.sort(tempList, new MyComparator());
		int[] outputArray = new int[size];
		for(int i=0;i<size;i++){
			SortObject so = tempList.get(i);
			outputArray[i] = so.getPosition();
		}
		return outputArray;
	}
	
}
